using Share;
using System.Collections.Generic;
using UnityEngine;

namespace Share
{
    public class LineRenderManager : SingletonMonoObject<LineRenderManager>
    {
        private Queue<LineRenderer> linePool = new Queue<LineRenderer>();
        private List<LineRenderer> activeLines = new List<LineRenderer>();
        private Transform poolParent;

        // 初始化对象池
        public override void DoInit()
        {
            base.DoInit();
            poolParent = transform;
            poolParent.SetParent(transform);
        }

        // 申请一个LineRenderer（2D游戏专用）
        public LineRenderer GetLine()
        {
            LineRenderer line;

            if (linePool.Count > 0)
            {
                // 从池子中取出
                line = linePool.Dequeue();
                line.gameObject.SetActive(true);
            }
            else
            {
                // 池子为空，创建新的LineRenderer
                GameObject lineObject = new GameObject("LineRenderer");
                line = lineObject.AddComponent<LineRenderer>();
                line.transform.SetParent(poolParent);
                
                // 设置2D游戏专用属性
                line.material = new Material(Shader.Find("Sprites/Default"));
                line.startWidth = 0.05f;
                line.endWidth = 0.05f;
                line.useWorldSpace = true;
                line.alignment = LineAlignment.TransformZ; // 2D游戏使用TransformZ对齐
            }

            // 添加到活跃列表
            activeLines.Add(line);
            return line;
        }

        // 释放LineRenderer回池子
        public void ReleaseLine(LineRenderer line)
        {
            if (line == null) return;

            // 从活跃列表中移除
            if (activeLines.Contains(line))
            {
                activeLines.Remove(line);
            }

            // 重置LineRenderer状态
            line.positionCount = 0;
            line.gameObject.SetActive(false);

            // 放回池子
            linePool.Enqueue(line);
        }

        // 批量释放所有活跃的LineRenderer
        public void ReleaseAllLines()
        {
            for (int i = activeLines.Count - 1; i >= 0; i--)
            {
                ReleaseLine(activeLines[i]);
            }
        }

        // 预创建一定数量的LineRenderer（2D游戏专用）
        public void PrewarmPool(int count)
        {
            for (int i = 0; i < count; i++)
            {
                GameObject lineObject = new GameObject("LineRenderer");
                LineRenderer line = lineObject.AddComponent<LineRenderer>();
                line.transform.SetParent(poolParent);
                
                // 设置2D游戏专用属性
                line.material = new Material(Shader.Find("Sprites/Default"));
                line.startWidth = 0.05f;
                line.endWidth = 0.05f;
                line.useWorldSpace = true;
                line.alignment = LineAlignment.TransformZ; // 2D游戏使用TransformZ对齐
                line.gameObject.SetActive(false);

                linePool.Enqueue(line);
            }
        }

        // 获取池子状态信息
        public void GetPoolStatus(out int poolCount, out int activeCount)
        {
            poolCount = linePool.Count;
            activeCount = activeLines.Count;
        }

        // 清理池子（释放所有对象）
        public void ClearPool()
        {
            // 释放所有活跃的线
            ReleaseAllLines();

            // 销毁池子中的所有对象
            while (linePool.Count > 0)
            {
                LineRenderer line = linePool.Dequeue();
                if (line != null && line.gameObject != null)
                {
                    Destroy(line.gameObject);
                }
            }

            linePool.Clear();
            activeLines.Clear();
        }
    }
}
